package com.lzs.esubscribe.service.impl;

import com.lzs.esubscribe.dto.LoginDTO;
import com.lzs.esubscribe.dto.ModifyPasswordDTO;
import com.lzs.esubscribe.dto.RegisterDTO;
import com.lzs.esubscribe.dto.UrlListDTO;
import com.lzs.esubscribe.entity.ESUrl;
import com.lzs.esubscribe.entity.ESUser;
import com.lzs.esubscribe.mapper.UrlMapper;
import com.lzs.esubscribe.mapper.UserMapper;
import com.lzs.esubscribe.service.IUserService;
import com.lzs.esubscribe.utils.TimeStampUtils;
import com.lzs.esubscribe.utils.TreeUtils;
import com.lzs.esubscribe.utils.UuidDoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lzs
 * @Date: 2022-02-17
 * @Description：用户业务接口实现
 */
@Service("IUserService")
public class UserServiceImpl implements IUserService {
    @Autowired
    UuidDoUtils uuidDoUtils;
    @Autowired
    TimeStampUtils timeStampUtils;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UrlMapper urlMapper;
    @Autowired
    TreeUtils treeUtils;

    @Override
    public int register(RegisterDTO registerDTO) {
        if(registerDTO.getAccount()==null||registerDTO.getPassword()==null){
            return 0;
        }

        Map<String, Object> postMap = new HashMap<>();
        postMap.put("uuid", uuidDoUtils.uuidDoGenerate());
        postMap.put("account", registerDTO.getAccount());
        postMap.put("password", registerDTO.getPassword());
        postMap.put("creat_time", timeStampUtils.getTimestamp());
        postMap.put("update_time", timeStampUtils.getTimestamp());
        postMap.put("nickname",registerDTO.getNickname());
        //检测用户名重复
        if (userMapper.checkUserExist(postMap) == 0) {
            return userMapper.register(postMap);
        } else {
            return 0;
        }
    }

    @Override
    public ESUser login(LoginDTO loginDTO) {
        if(loginDTO.getAccount()==null||loginDTO.getPassword()==null){
            return null;
        }
        // 登录验证
        ESUser loginUser = userMapper.login(loginDTO);
        if (loginUser != null) {
            Map<String,Object> updateMap = new HashMap<>();
            updateMap.put("timeStamp",timeStampUtils.getTimestamp());
            updateMap.put("account",loginDTO.getAccount());
            // 更新登录
            userMapper.updateLoginInfo(updateMap);
        }
        return loginUser;
    }

    @Override
    public List<UrlListDTO> getUserSubscribeTree(String uuid) {
        List<UrlListDTO> userUrlList = urlMapper.getUserAllUrl(uuid);
        // 寻找所有根节点
        List<UrlListDTO> rootList = treeUtils.findRoot(userUrlList);
        // 递归寻找子节点
        for(UrlListDTO node : rootList){
            treeUtils.findChild(node,userUrlList);
        }
        return rootList;
    }

    @Override
    public int modifyPassword(ModifyPasswordDTO modifyPasswordDTO) {
        int resCheckOldPassword = userMapper.checkOldPassword(modifyPasswordDTO);
        if(resCheckOldPassword!=0){
            return userMapper.modifyPassword(modifyPasswordDTO);
        }else {
            return 0;
        }
    }
}
